package com.woniuxy.happylife.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.happylife.conf.CommentsFeginClient;
import com.woniuxy.happylife.entity.MallGoods;
import com.woniuxy.happylife.entity.MallRemark;
import com.woniuxy.happylife.entity.ResponseEntity;
import com.woniuxy.happylife.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author:roger
 * @Date:2022/2/17 15:08:11
 * @Description:
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    GoodsService goodsService;

    @Autowired
    CommentsFeginClient commentsFeginClient;

    @GetMapping("/{pageNum}/{pageSize}")
    public ResponseEntity<PageInfo<MallGoods>> getAll(@PathVariable("pageNum") int pageNum,@PathVariable("pageSize") int pageSize){
        PageHelper.startPage(pageNum,pageSize);
        PageInfo<MallGoods> goodsPages = new PageInfo<>(goodsService.findAll());
        return new ResponseEntity<PageInfo<MallGoods>>(200,"查到所有商品数据",goodsPages);
    }
    @GetMapping("/")
    public ResponseEntity<List<MallGoods>> getByCid(@RequestParam("cid") int cid){
        return new ResponseEntity<>(200,"按照分类查询商品", goodsService.findByCid(cid));
    }
    @GetMapping("/hot")
    public  ResponseEntity<PageInfo<MallGoods>> getHot(@RequestParam("pageNum") int pNum,@RequestParam("pageSize") int pSize){
        PageHelper.startPage(pNum,pSize);
        PageInfo<MallGoods> goodsPages = new PageInfo<>(goodsService.findAll());
        return new ResponseEntity<PageInfo<MallGoods>>(200,"获取最新的几个商品",goodsPages);
    }
    @PostMapping("/")
    public ResponseEntity<Integer> publish(@RequestBody MallGoods goods){
        return new ResponseEntity(200,"商品发布成功",goodsService.add(goods));
    }
    @PutMapping("/{gid}")
    public ResponseEntity<Integer> updateStatusById(@PathVariable("gid") int gid){
        return new ResponseEntity<Integer>(200,"商品审核成功",goodsService.updateStatusById(gid));
    }

    //远程调用评论微服务的根据id查询
    @GetMapping("/{gid}")
    public ResponseEntity<MallGoods> findDetailById(@PathVariable("gid")int gid){
        MallGoods mallGoods= goodsService.findById(gid);
        //设置商品的评论列表
        ResponseEntity<List<MallRemark>> res = commentsFeginClient.getBygid(gid);
        mallGoods.setMallRemarks(res.getData());
        //设置评论数
        ResponseEntity<Integer> resp = commentsFeginClient.getNumsBygid(gid);
        mallGoods.setRemarknums(resp.getData());
        return new ResponseEntity<MallGoods>(200,"商品详情获取成功",mallGoods);
    }
    @GetMapping("/byName/{name}")
    public ResponseEntity<List<MallGoods>> findGoodsByName(@PathVariable("name") String name){
        return new ResponseEntity<List<MallGoods>>(200,"按照名字查询成功",goodsService.findByName(name));
    }
}
